#include<iostream>

struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};

void Inorder(struct TreeNode* root, int* num, int* max, int* count)
{
    if (!root)
    {
        return;
    }
    (*num)++;
    if (*max < *num)
    {
        *count = 0;
        *max = *num;
    }
    Inorder(root->left, num, max, count);
    Inorder(root->right, num, max, count);
    if (*max == *num)
    {
        *count += root->val;
    }
    (*num)--;
}
int deepestLeavesSum(struct TreeNode* root) {
    int num = 0, max = 0, count = 0;
    Inorder(root, &num, &max, &count);
    return count;
}